<template>

</template>

<script>
import c2s from '~/message/protos/c2s_pb'
import s2c from '~/message/protos/s2c_pb'

export default {
    name: "AdminSocket",
    data() {
        return {

        };
    },
    components: {

    },
    computed: {
        userInfo() {
            return this.$store.state.manager.info;
        },
    },
    methods: {
        logout() {
            this.$store.dispatch('manager/Logout').then(() => {
                this.$message.success('退出登录');
                this.$router.push({name: 'login'});
                // 关闭socket
                this.$ws.clear();
            });
        },
        initWebsocket() {
            this.$ws.clear();
            this.$ws.addHandlerClose(() => {
                console.log("socket Close, retry reconnection");
                setTimeout(() => {
                    this.$ws.init();
                }, 10000)
            });

            // 初始化完毕
            this.$ws.addHandler(s2c.SC.CONNECT_INIT, s2c.ConnectInit, connectInit => {
                console.log(connectInit.getClientId())
                console.log("Socket连接成功");
                let loginRequest = new c2s.LoginRequest();
                loginRequest.setToken(this.$store.state.manager.token);

                this.$ws.send(c2s.CS.LOGIN_REQUEST, loginRequest);
            });
            this.$ws.addHandler(s2c.SC.CONNECT_PING, s2c.ConnectPing, connectPing => {
                // console.log(connectPing.getTime())
            })
            this.$ws.addHandler(s2c.SC.CONNECT_RESET, s2c.ConnectReset, connectReset => {
                // 收到服务端要求重连的请求
                console.log("服务器关闭, 尝试重连");
                setTimeout(() => {
                    this.$ws.init();
                }, 10000);
            })
            this.$ws.addHandler(s2c.SC.LOGIN_RESPONSE, s2c.LoginResponse, loginResponse => {
                if (loginResponse.getCode() === s2c.CODE.FAIL) {
                    this.$notify.error("Socket登录失败");
                    return ;
                }
                this.$notify.success("Socket登录成功");
            })
            this.$ws.addHandler(s2c.SC.ADMIN_RELOAD, s2c.AdminReload, adminReload => {
                if (adminReload.getForce()) {
                    setTimeout(() => {
                        location.reload();
                    }, 10000);
                    this.$confirm(adminReload.getContent() || '检测到后台有更新, 10秒后将自动刷新', '系统更新', {closeOnClickModal: false, showClose: false, showCancelButton: false,});
                    return ;
                }
                this.$confirm(adminReload.getContent() || '检测到后台有更新, 是否立即刷新?', '系统更新', {closeOnClickModal: false,}).then(() => {
                    location.reload();
                });
            });
            // 模型变动通知
            // this.$ws.addHandler(s2c.SC.MODEL_UPDATED, s2c.ModelUpdated, modelUpdated => {
            //     this.$store.commit('backend/SET_MODEL_UPDATED', modelUpdated)
            // })
            this.$ws.addHandler(s2c.SC.ADMIN_FORCE_LOGOUT, s2c.ResponseMsg, responseMsg => {
                this.logout();
            })
            this.$ws.addHandler(s2c.SC.COMMAND_READLINE_RESPONSE, s2c.CommandReadlineResponse, commandReadlineResponse => {
                this.$store.commit("goservice/PUSH_COMMAND_HISTORY", commandReadlineResponse)
            })
            this.$ws.init();
        }
    },
    created() {
        if (this.$admin.enable_socket) this.initWebsocket();
    }
}
</script>

<style lang="scss" scoped>
::v-deep .command-window {
    background: black;
    color: white;
    .el-dialog__title {
        color: white;
    }
    .el-dialog__body {
        max-height: 40vh;
        overflow-y: auto;
    }

}
</style>
